import 'package:flutter/material.dart';
import 'package:helloworld/res/depositData.dart';
import 'package:helloworld/tabs.dart';
import 'package:dio/dio.dart';
import 'dart:ui';
class DepositDetail extends StatefulWidget{
  final arguments;
  DepositDetail({this.arguments});
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _DepositDetail(arguments:this.arguments);
  }
}

class _DepositDetail extends State<DepositDetail>{
  var _count = TextEditingController();
  var _countData;
  var _number = TextEditingController();
  var _numberData;
  var _deposit = TextEditingController();
  var _depositData;
  Brightness brightnessValue ;
  final arguments;
  _DepositDetail({this.arguments});
  _deleteData()async{
    await Dio().post("http://123.207.114.127:1314/deposit/deleteDeposit",queryParameters: {
      'depositid':depositData[this.arguments['id']]['depositid']
    });
  }
  _delete(){
    showDialog(context: context,builder: (context){
      return AlertDialog(
        content: Text('您确定要删除吗?',style: TextStyle(fontSize: 20),),
        actions: <Widget>[
          FlatButton(
            child: Text('取消',style: TextStyle(fontSize: 20),),
            onPressed: (){
              Navigator.pop(context);
            },
          ),
          FlatButton(
            child: Text('确定',style: TextStyle(fontSize: 20),),
            onPressed: (){
              Navigator.pop(context);
              _deleteData();
              Future.delayed(Duration(milliseconds: 50), (){
                Navigator.of(context).pop();
                Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new Tabs( index:1)), (route) =>route==null);
              });
            },
          ),

        ],
      );
    });
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _count.text = depositData[this.arguments['id']]['depositcount'];
    _countData = depositData[this.arguments['id']]['depositcount'];
    _number.text = depositData[this.arguments['id']]['depositnumber'];
    _numberData = depositData[this.arguments['id']]['depositnumber'];
    _deposit.text = depositData[this.arguments['id']]['deposit'].toStringAsFixed(2);
    _depositData = depositData[this.arguments['id']]['deposit'].toStringAsFixed(2);
    brightnessValue = window.platformBrightness;
  }
  updateDeposit()async{
    Dio().post("http://123.207.114.127:1314/deposit/updateDeposit",queryParameters: {
      'depositid':depositData[this.arguments['id']]['depositid'],
      'depositcount':_countData,
      'depositnumber':_numberData,
      'deposit': double.parse(_depositData)
    });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        title: Text('编辑存款'),
        actions: <Widget>[
          InkWell(
            child: Padding(
              child:Text('保存',style: TextStyle(color: Colors.white,fontSize: 20),),
              padding: EdgeInsets.all(16),
            ),
            onTap: (){
              updateDeposit();
              Future.delayed(Duration(milliseconds: 50), (){
                Navigator.of(context).pop();
                Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new Tabs( index:1)), (route) =>route==null);
              });
            },
          )
        ],
      ),
      body:  Padding(
        padding: EdgeInsets.all(20),
        child:SingleChildScrollView(
          child:  Column(
            children: <Widget>[
              TextField(
                keyboardAppearance: brightnessValue,
                controller: _count,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: '账户'
                ),
                onChanged: (value){
                  setState(() {
                    _countData = value;
                  });

                },
              ),
              // 10
              SizedBox(height: 10,),
              TextField(
                keyboardAppearance: brightnessValue,
                keyboardType: TextInputType.number,
                controller: _number,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: '尾号'
                ),
                onChanged: (value){
                  setState(() {
                    _numberData = value;
                  });

                },
              ),
              //10
              SizedBox(height: 10,),
              TextField(
                keyboardAppearance: brightnessValue,
                keyboardType: TextInputType.numberWithOptions(decimal: true),
                controller: _deposit,
                decoration: InputDecoration(
                    border: OutlineInputBorder(),
                    labelText: '金额'
                ),
                onChanged: (value){
                  setState(() {
                    _depositData = value;
                  });

                },
              ),
//            SizedBox(height: 50,), iphone11
              SizedBox(height: 50,),
              Container(
                width: 150,
                height: 49,
                child: RaisedButton(
                  color: Colors.lightBlueAccent,
                  textColor: Colors.white,
                  child: Text('删除',style: TextStyle(fontSize: 20),),
                  onPressed: _delete,
                ),
              )

            ],
          ),
          scrollDirection: Axis.vertical,
        ),
      ),
    );
  }
}